/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Classes;

import com.sun.jersey.api.client.WebResource;
import Banks.*;
import Users.*;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author xiph
 */
public class DatabaseService
{
	public static Banks fetchBanks()
	{
		Banks banks = null;
		try
		{
			URL url = new URL( "http://home.emilan.se:8084/rs/admin/bank" );
			JAXBContext jc = JAXBContext.newInstance(Banks.class);
			Unmarshaller u = jc.createUnmarshaller();
			banks = (Banks) u.unmarshal(url);
		}
		catch (MalformedURLException ex)
		{
			
		}
		catch(JAXBException ex)
		{
			
		}
		return banks;
	}
	
	public static Users fetchUsers()
	{
		Users users = null;
		try
		{
			URL url = new URL( "http://home.emilan.se:8084/rs/admin/user" );
			JAXBContext jc = JAXBContext.newInstance(Users.class);
			Unmarshaller u = jc.createUnmarshaller();
			users = (Users) u.unmarshal(url);
		}
		catch (MalformedURLException ex)
		{
			System.out.println(ex.toString());
		}
		catch(JAXBException ex)
		{
			System.out.println(ex.toString());
		}
		return users;
	}
	
	public static void updateBank(Bank b)
	{
		StringWriter sw = new StringWriter();
		JAXBContext jc;
		try
		{
			jc = JAXBContext.newInstance(Bank.class);
			Marshaller m = jc.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			m.marshal(b, sw);
		}
		catch (JAXBException ex)
		{
			System.out.println(ex);
		}
		
			Client client = Client.create();
			WebResource webResource = client.resource("http://home.emilan.se:8084/rs/admin/bank");
			webResource.type("application/xml").put(ClientResponse.class, sw.toString());
	}
	
	public static void updateUser(User u)
	{
		StringWriter sw = new StringWriter();
		JAXBContext jc;
		try
		{
			jc = JAXBContext.newInstance(User.class);
			Marshaller m = jc.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			m.marshal(u, sw);

			Client client = Client.create();
			WebResource webResource = client.resource("http://home.emilan.se:8084/rs/admin/user");
			webResource.type("application/xml").put(ClientResponse.class, sw.toString());
		}
		catch (JAXBException ex)
		{
			System.out.println(ex);
		}
	}
	
	public static void deleteUser(User u)
	{
			String id = u.getId()+"";
			Client client = Client.create();
			WebResource webResource = client.resource("http://home.emilan.se:8084/rs/admin/user/"+id);
			webResource.delete();
	}
	
	public static void deleteBank(Bank b)
	{
			String id = b.getId()+"";
			Client client = Client.create();
			WebResource webResource = client.resource("http://home.emilan.se:8084/rs/admin/bank/"+id);
			webResource.delete();
	}
        
    public static void addBank(Bank b) {
        StringWriter sw = new StringWriter();
        JAXBContext jc;
        try {
            jc = JAXBContext.newInstance(Bank.class);
            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(b, sw);
        } catch (JAXBException ex) {
            System.out.println(ex);
        }

        Client client = Client.create();
        WebResource webResource = client.resource("http://home.emilan.se:8084/rs/admin/bank");
        webResource.type("application/xml").post(ClientResponse.class, sw.toString());
    }
}